/*
 * Copyright (C) 2024-2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyia@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyia@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef DBUS_INVOKER_FACTORY
#define DBUS_INVOKER_FACTORY

#include "devcommon_global.h"
#include "invokerinterface.h"

#include <QtDBus/QDBusReply>

// 简化DBUS调用方法的写法
#define DBUS_BLOCK_INVOKE(interface, funName, ...) interface->Invoke(funName, {__VA_ARGS__}, BlockMode::BLOCK)
#define DBUS_INVOKE(interface, funName, ...) interface->Invoke(funName, {__VA_ARGS__}, BlockMode::BLOCKWITHGUI)
#define DBUS_NOBLOCK_INVOKE(interface, funName, ...) interface->Invoke(funName, {__VA_ARGS__}, BlockMode::NOBLOCK)
#define GET_MESSAGE_VALUE(type, output, message) type output = QDBusReply<type>(message).value()

// implement
class DEVCOMMONSHARED_EXPORT InvokerFactory : public QObject
    , public InvokerFactoryInterface
{
    Q_OBJECT
public:
    static InvokerFactory &GetInstance()
    {
        static InvokerFactory instance;
        return instance;
    }
    virtual DBusInvokerInterface *CreateInvoker(const QString &service, const QString &path, const QString &interface, ConnectType type = ConnectType::SESSION, QObject *parent = nullptr) override;
    virtual SettingsInvokerInterface *CreateSettings(const QByteArray &schema_id, const QByteArray &path = QByteArray(), QObject *parent = nullptr) override;

public:
    void setInvokerInteface(const QString &, DBusInvokerInterface *);
    void setSettinsInteface(const QString &, SettingsInvokerInterface *);

private:
    InvokerFactory();
    InvokerFactory(InvokerFactory &);
    InvokerFactory &operator=(const InvokerFactory &);
    ~InvokerFactory() override;

    QMap<QString, DBusInvokerInterface *> m_intefaceMap;
    QMap<QString, SettingsInvokerInterface *> m_settingsMap;
};

#endif
